import XMonad
-- Hooks

import XMonad.Hooks.EwmhDesktops
import XMonad.Hooks.ManageDocks
-- Layout

import XMonad.Hooks.WindowSwallowing
import XMonad.Layout.Magnifier
import XMonad.Layout.NoBorders
import XMonad.Layout.Renamed ()
import XMonad.Layout.Spacing
import XMonad.Layout.ThreeColumns
import XMonad.Layout.ToggleLayouts ()
import XMonad.Util.EZConfig
import XMonad.Hooks.ManageHelpers
-- import Data.Generics
-- import qualified DBus.Client as D
-- import XMonad.Hooks.DynamicLog

-- :::::::::::::::::::::::::::::::::::::
-- main
-- :::::::::::::::::::::::::::::::::::::
main =
  xmonad $
  docks $
  ewmh $
  myConfig

-- :::::::::::::::::::::::::::::::::::::
-- custom myConfig
-- :::::::::::::::::::::::::::::::::::::
myConfig =
  def
    { modMask = mod4Mask, -- Rebind Mod to the Super key
      layoutHook = myLayout, -- use custom layouts
      startupHook = myStartupHook, -- autostart
      manageHook = myManageHook,
      workspaces = myWorkspaces,
      terminal = "alacritty",
      normalBorderColor = "#000000",
      focusedBorderColor = "#b0e0e6",
      handleEventHook = myHandleEvenHook
    }
    -- :::::::::::::::::::::::::::::::::::::::
    -- custom keybind --> Xmonad.Util.EZconfig
    -- :::::::::::::::::::::::::::::::::::::::
    `additionalKeysP`
    -- ...............................................
    [ ("M-S-x", spawn "~/.config/rofi/powermenu/powermenu.sh"), -- 系统登出
      ("M-d", spawn "rofi -show drun -theme ~/.config/rofi/theme/dracula.rasi"),
      ("M-S-d", spawn "dmenu_run"), -- 默认情况下找到的软件多于rofi
      ("M-q", kill), -- 关闭当前窗口
      ("M-S-q", kill),
      ("M-<Return>", spawn "alacritty"),
      -- 打开应用 M-o
      -- ...............................................
      ("M-o c", spawn "~/software/clash/cfw"),
      ("M-o e", spawn "emacs"),
      ("M-S-e", spawn "emacsclient --eval '(emacs-everywhere)'"),
      ("M-o p", spawn "alacritty -e ~/software/pycharm-2023.2.1/bin/pycharm.sh"),
      ("M-e", spawn "~/.local/bin/neovide-lunarvim"),
      -- video
      ("M-o v d", spawn "video-downloader"), -- 视频下载工具
      ("M-o v v", spawn "vlc"), -- 看视频工具
      -- warpd
      ("M-o M-k", spawn "warpd"),
      -- sound
      ("M-o s c", spawn "pavucontrol"),
      -- picture
      ("M-o p r", spawn "ristretto"), -- 图片查看器
      -- browser
      ("M-o w", spawn "brave"), -- browser
      -- file
      ("M-o f", spawn "thunar"), -- file manage
      -- mail
      ("M-o m", spawn "mailspring"),
      -- tool
      ("M-o t x p", spawn "xfce4-power-manager-settings"), -- xfce4 电源管理工具
      ("M-o t x n", spawn "xfce4-notifyd-config"), -- xfce 通知管理
      ("M-o t w", spawn "nm-connection-editor"), -- wifi
      -- bar
      -- note
      ("M-o n x", spawn "xournalpp"),
      ("M-o l d", spawn "libreoffice --draw"),
      ("M-o l w", spawn "libreoffice --writer"),
      ("M-o l s", spawn "libreoffice"),
      ("M-o l c", spawn "libreoffice --calc"),
      -- ...............................................
      -- , ("M-f"                     , sendMessage ToggleStruts) -- 切换xmobar的显示和隐藏 ToggleStruts 由 ManageDocks提供
      ("M-f", spawn "polybar-msg cmd toggle"), -- 切换xmobar的显示和隐藏 ToggleStruts 由 ManageDocks提供
      -- time manager
      ("M-o t p", spawn "pomodoro-logger"),
      -- fn + <f1>...<f12>
      -- ...............................................
      -- sound
      ("<XF86AudioLowerVolume>", spawn "~/.config/dunst/scripts/sound_down"), -- 降低音量
      ("<XF86AudioRaiseVolume>", spawn "~/.config/dunst/scripts/sound_up"), -- 提高音量
      ("<XF86AudioMute>", spawn "amixer -D pulse set Master 1+ toggle"), -- 打开/关闭声音
      -- light
      ("<XF86MonBrightnessUp>", spawn "xbacklight -inc 5"), -- 提高亮度
      ("<XF86MonBrightnessDown>", spawn "xbacklight -dec 5"), -- 降低亮度
      -- 截图
      ("<Print>", spawn "flameshot gui"),
      -- 切换layout
      -- ...............................................
      ("M-n", sendMessage $ JumpToLayout "Full"),
      -- xmonad M-x
      -- ...............................................
      -- command
      ("M-x c p", spawn "polybar-msg cmd restart"),
      ("M-x c s", spawn "xmonad --restart"),
      ("M-x c r", spawn "xmonad --recompile; xmonad --restart")
    ]

-- :::::::::::::::::::::::::::::::::::::
-- custom windows layout
-- :::::::::::::::::::::::::::::::::::::
-- avoidStruts 由 XMonad.Hook.ManageDocks 提供，确保窗口不会遮挡状态栏
myLayout = avoidStruts (tiled ||| full ||| Mirror tiled ||| threecol)
  where
    full = noBorders Full
    tiled = spacingWithEdge 4 $ Tall nmaster delta ratio
    threecol = spacingWithEdge 4 $ magnifiercz' 1.2 $ ThreeCol nmaster delta ratio -- by XMonad.Layout.Magnifier
    nmaster = 1 -- Default number of windows in the master pane
    ratio = 1 / 2 -- Default proportion of screen occupied by master pane
    delta = 3 / 100 -- Percent of screen to increment by when resizing panes

-- :::::::::::::::::::::::::::::::::::::
-- startUpHook
-- :::::::::::::::::::::::::::::::::::::
myStartupHook = do
  spawn "$HOME/.config/xmonad/scripts/start.sh"
  spawn "picom -b --config $HOME/.config/picom/picom.conf"

-- :::::::::::::::::::::::::::::::::::::
-- manageHook
-- :::::::::::::::::::::::::::::::::::::
myManageHook = composeAll
    [className =? "Archlinux-logout.py" --> doFloat]

-- :::::::::::::::::::::::::::::::::::::
-- custom workspaces
-- :::::::::::::::::::::::::::::::::::::
-- myWorkspaces    = ["\61612","\61899","\61947","\61635","\61502","\61501","\61705","\61564","\62150","\61872"]
-- myWorkspaces = ["\61728", "\62057", "\61564", "\62150", "\61502", "\61501", "\61484", "\61473", "\61450"]
myWorkspaces = ["TE", "BR", "FI", "ME", "PI", "VE", "OF", "TI", "VP"]

-- myWorkspaces    = ["1","2","3","4","5","6","7","8","9","10"]
-- myWorkspaces    = ["I","II","III","IV","V","VI","VII","VIII","IX","X"]

-- :::::::::::::::::::::::::::::::::::::
-- handleEventHook
-- :::::::::::::::::::::::::::::::::::::
myHandleEvenHook = swallowEventHook (className =? "Alacritty" <||> className =? "XTerm") (return True) -- 终端打开软件时被隐藏

